// #ifdef H5
import {
	http
} from '@/js_sdk/luch-request/index.js' // 全局挂载引入，配置相关在该index.js文件里修改

import indexConfig from '@/config/index.config.js'
import jweixin from 'jweixin-module'

const openWeixinUserinfo = function(REDIRECT_URI) {
	// console.log(`${indexConfig.baseApiUrl}wx/oauth?appid=wxad315a32195243f2&callback=${REDIRECT_URI}`)
	// window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxad315a32195243f2&redirect_uri=${REDIRECT_URI}&response_type=code&scope=snsapi_userinfo&state=wxad315a32195243f2#wechat_redirect`;
	// `${indexConfig.baseApiUrl}wx/oauth?appid=wxad315a32195243f2&callback=${REDIRECT_URI}`;
	// window.location.replace(`${indexConfig.baseApiUrl}wx/oauth?appid=wxad315a32195243f2&response_type=code&scope=snsapi_userinfo&state=wxad315a32195243f2&callback=${REDIRECT_URI}#wechat_redirect`)
	const appID = 'wx815f741fab67c8b4'; //公众号appID
	// const callBack = 'https%3A%2F%2Fh5.yulinkeji.cn%2Fh5%2Findex.html%23%2Fpages%2Findex%2Findex'; //回调地址 就是你的完整地址登录页
	// const callBack = window.location.href; //回调地址 就是你的完整地址登录页
	// console.log(callBack, 'callBack')
	//通过微信官方接口获取code之后，会重新刷新设置的回调地址【redirect_uri】
	setTimeout(function() {
	    window.location.replace('https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
	    	appID + '&redirect_uri=' + REDIRECT_URI + //看后端给你的是否已经转码 转码了就不用encodeURIComponent
	    	'&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect')
	}, 0);
	
}

export default {
	// 判断是否在微信中
	isWechat: function() {
		const ua = window.navigator.userAgent.toLowerCase()
		// console.log(ua,"uauaauauauau") 
		if (ua.match(/micromessenger/i) == 'micromessenger') {
			return true
		} else {
			return false
		}
	},
	isMobile: function(){//是否是手机浏览器
	  let isMobel=/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
	  let isPad=window.matchMedia("(max-width: 767px)").matches;
	  return isMobel&&isPad;
	},
	// 初始化sdk配置
	initJssdk: function(callback, url) {
		// 服务端进行签名 ，可使用uni.request替换。 签名算法请看文档
		http.post('jssdk/ticket', {
			url: url
		}).then(res => {
			const result = res.data
			console.log(result,"result")
			if (res.code == 200) {
				jweixin.config({
					debug: false,
					appId: 'wx815f741fab67c8b4',
					timestamp: result.timestamp,
					nonceStr: result.nonceStr,
					signature: result.signature,
					jsApiList: [
						'checkJsApi',
						// 'onMenuShareTimeline',
						// 'onMenuShareAppMessage',
						//微信分享改版，最新方法，上面方法即将废弃
						'updateAppMessageShareData',
						'updateTimelineShareData',
						'wx-open-launch-app',
						'launchApplication',
						'hideOptionMenu',
						'showOptionMenu',
						'wx-open-launch-weapp'
					],
					openTagList: ['wx-open-launch-app', 'wx-open-launch-weapp']
				})
				// 配置完成后，再执行分享等功能
				if (callback) {
					callback(res.result)
				}
			}
		})
	},
	// 在需要自定义分享的页面中调用
	share: function(data, url, wxHandler) {
		console.log(data, url, wxHandler, "_shareData3333333333333")
		url = url || window.location.href
		console.log(data, url, wxHandler, "_shareData4444444444")
		if (!this.isWechat()) {
			return
		}
		// 每次都需要重新初始化配置，才可以进行分享
		this.initJssdk(function(signData) {
			jweixin.ready(function() {
				const shareData = {
					title: data && data.title ? data.title : signData.site_name,
					desc: data && data.desc ? data.desc : signData.site_description,
					link: url,
					imgUrl: data && data.img ? data.img : signData.site_logo,
					success: function(res) {
						// 用户点击分享后的回调，这里可以进行统计，例如分享送金币之类的
						// request.post('/api/member/share'); 
					},
					cancel: function(res) {}
				}
				// 分享给朋友接口
				// jweixin.onMenuShareAppMessage(shareData)
				jweixin.updateAppMessageShareData(shareData)
				// 分享到朋友圈接口
				// jweixin.onMenuShareTimeline(shareData)
				jweixin.updateTimelineShareData(shareData)
				// 微信sdk ready 后处理  
				wxHandler(jweixin)
			})
		}, url)
	},
	// 在需要定位页面调用
	location: function(callback) {
		if (!this.isWechat()) {
			console.log('不是微信客户端')
			return
		}
		this.initJssdk(function(res) {
			jweixin.ready(function() {
				console.info('定位ready')
				jweixin.getLocation({
					type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
					success: function(res) {
						// console.log(res);
						// thisPageUrl=thisPageUrl.replace("&", "%26");
						callback(res)
					},
					fail: function(res) {
						console.log(res)
					}
					// complete:function(res){
					//     console.log(res)
					// }
				})
			})
		})
	},
	wxauth: function(wxcode) {
		let REDIRECT_URI = window.location.href
		REDIRECT_URI = encodeURIComponent(REDIRECT_URI)
		// const wxcode = getQueryString('code');
		if (!wxcode) {
			openWeixinUserinfo(REDIRECT_URI)
		}
	}
}
// #endif